package com.dong.redis.service.impl;

import com.dong.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @description: redis 业务实现类
 * @author: 陈瑞东
 * @create: 2022-05-31 15:06
 */
public class RedisServiceImpl implements RedisService {

    @Autowired
    @Qualifier(value = "jedisPoolMap")
    private Map<Integer, JedisPool> jedisPoolMap;


    @Override
    public Jedis getResource(Integer db) {
        if (!jedisPoolMap.containsKey(db)) {
            return null;
        }
        return jedisPoolMap.get(db).getResource();
    }

    @Override
    public void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}
